// Copyright 2012 Linuxthink. All rights reserved.
// The Word Filter Project
// wf_io.cpp
// All the file I/O functions are here

#include <iostream>
#include <string>
#include <list>
#include <fstream>
using namespace std;
  
// Reads wp_configuration.cfg and gets the value for input_file, output_file, 
// dictionary_file, valid_word, will_sort. Use defalut value if the
// configuration file cannot be found.
bool ReadConfiguration(string &input_file,
                       string &output_file,
                       string &dictionary_file,
                       string &valid_word,
                       bool &will_sort) {
  ifstream fin("wf_configuration.cfg", ifstream::in);
  if (fin.fail()) { // Default configuration
    cout << "Cannot open wf_configuration.cfg, will use default settings." 
         << endl;
    input_file  = "input.txt",
    output_file = "output.txt",
    dictionary_file = "dictionary.txt",
    valid_word = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
    will_sort = 0;
  } else { // Read wf_configuration
    string temp_string = "";
    while (fin >> temp_string)  {
      if (temp_string == "input_file") {
        fin >> temp_string;
        fin >> input_file;
      } else if (temp_string == "output_file") {
        fin >> temp_string;
        fin >> output_file;
      } else if (temp_string == "dictionary_file") {
        fin >> temp_string;
        fin >> dictionary_file;
      } else if (temp_string == "valid_word"){
        fin >> temp_string;
        fin >> valid_word;
      } else if (temp_string == "will_sort") {
        fin >> temp_string;
        fin >> will_sort;
      }    
    }
  }
  fin.close();
  return true;
}

bool ReadInput(string input_file, list<string> &word_list) {
  // First convert the input_file in to "const char *"!!!!
  char *input_file_pt = &input_file[0];
  ifstream fin(input_file_pt, ifstream::in);
  if (fin.fail()) {
    cout << "Cannot open the input file, will terminate." << endl;
    return false;
  }
  string temp_string;
  int count = 0;
  while (fin >> temp_string) {
    word_list.push_back(temp_string);
    count++;
  }
  fin.close();
  cout << "Imported " << count << " raw words." << endl;
  return true;
}

bool ReadDictionary(string dictionary_file,
                    list<string> &dictionary_list) {
  // First convert the dictionary_file in to "const char *"!!!!
  char *dictionary_file_pt = &dictionary_file[0];
  ifstream fin(dictionary_file_pt, ifstream::in);
  if (fin.fail()) {
    cout << "Cannot open the dictionary file, will terminate." << endl;
    return false;
  }
  string temp_string;
  int count = 0;
  while (fin >> temp_string) {
    dictionary_list.push_back(temp_string);
    count++;
  }
  fin.close();
  cout << "Imported " << count << " dictionary words." << endl;
  return true;
}

bool WriteOutput(string output_file, list<string> &word_list) {
  // First convert the output_file in to "const char *"!!!!
  char *output_file_pt = &output_file[0];
  ofstream fout(output_file_pt);
  if (fout.fail()) {
    cout << "Cannot save the output file, check your disk, will terminate."
         << endl;
    return false;  
  }
  list<string>::iterator it = word_list.begin();
  int count = 0;
  for (it = word_list.begin(); it != word_list.end(); it++) {
    fout << *it << endl;
    count++;
  }
  fout.close();
  cout << "Outputted " << count << " filtered words." << endl;
  return true;
}






